package Server;

import java.sql.Date;

import Entities.Product;
import mysqlCnnection.CatalogueUpdate;
import mysqlCnnection.CustomersUpdate;
import mysqlCnnection.Reports;
import mysqlCnnection.SalesManController;
import mysqlCnnection.TemplateController;

/**
 * This class analyze client request and make the answer to client 
 * @author matan
 * @author sharon
 * @author ido
 * @author michael 
 */
public class ConnectionToUser 
{
	
	private String[] clientmessage = null;
	private Object messagetoclient = null;
	
	/**
	 * @param clientmessage
	 */
	public ConnectionToUser(String[] clientmessage) 
	{
		super();
		this.clientmessage = clientmessage;
	}

	/**
	 * @return the clientmessage
	 */
	public String[] getClientmessage() {
		return clientmessage;
	}
	
	/**
	 * This method update (messagetoclient) to product details or null on error 
	 */
	private void getProductDetails()
	{
		try
		{
			CatalogueUpdate catalogueupdate = new CatalogueUpdate();
			String[] productdetails = catalogueupdate.searchForProduct(Long.parseLong(clientmessage[1]));
			if (productdetails != null)
			{
				messagetoclient = productdetails;			
			}
		}catch(Exception e)	{};		
	}
	
	/**
	 * This method update (messagetoclient) to "OK" if product details successfully inserted,
	 * else to "IlegalCataloguenumber" 
	 */
	private void insertProductDetails()
	{
		Product product = new Product(0, null, null, 0);
		try
		{
			product.setPrice(Double.parseDouble(clientmessage[1]));
			product.setName(clientmessage[2]);
			product.setDescription(clientmessage[3]);
			product.setCataloguenumber(Long.parseLong(clientmessage[4]));			
			if (product.insertToDataBase())
			{
				messagetoclient = "OK";
			}
			else
				messagetoclient = "IlegalCataloguenumber";
		}catch(Exception e){};	
	}
	
	/**
	 * This method update (messagetoclient) to "OK" if product details successfully updated,
	 * else to "IlegalCataloguenumber" 
	 */
	private void updateProductDetails()
	{
		Product product = new Product(0, null, null, 0);
		try
		{
			product.setPrice(Double.parseDouble(clientmessage[1]));
			product.setName(clientmessage[2]);
			product.setDescription(clientmessage[3]);
			product.setCataloguenumber(Long.parseLong(clientmessage[4]));			
			if (product.updateProduct())
			{
				messagetoclient = "OK";
			}
			else
				messagetoclient = "IlegalCataloguenumber";
		}catch(Exception e){};		
	}
	
	
	/**
	 * This method update (messagetoclient) to "OK" if product successfully remove 
	 * from data base, else to "ERROR" 
	 */
	private void removeProduct()
	{
		try
		{
			CatalogueUpdate catalogueupdate = new CatalogueUpdate();
			messagetoclient = catalogueupdate.RemoveProduct(Long.parseLong(clientmessage[1]));
		}
		catch(NumberFormatException e)	
		{
			messagetoclient = "ERROR";
		};	
	}
	
	/**
	 * This method update (messagetoclient) to Long array of all products ID  
	 * or to null on error 
	 */
	private void loadAllProductsID()
	{
		try
		{
			CatalogueUpdate catalogueupdate = new CatalogueUpdate();
			messagetoclient = catalogueupdate.loadAllProductID();
		}catch (Exception except)
		{
			messagetoclient = null;
		}	
	}
	
	/**
	 * This method update (messagetoclient) to array of String of all catalog sections
	 * authorizations details or to null on error. 
	 */
	private void loadAllProductsNames()
	{
		try
		{	
			CatalogueUpdate catalogueupdate = new CatalogueUpdate();
			messagetoclient = catalogueupdate.loadAllProductsNames();	
		}catch (Exception except)
		{
			messagetoclient = null;
		}	
	}
	
	/**
	 * This method update (messagetoclient) to "OK" campaign successfully added  
	 * to data base, or to "IlegalID" on SQL error, else to null. 
	 */
	private void createTemplate(){
		try{
			TemplateController templateController = new TemplateController();
			String name, sector, message;
			long pID, cID;
			cID = Long.parseLong(clientmessage[1]);
			name = clientmessage[2];
			sector = clientmessage[3];
			message = clientmessage[4];
			pID = Long.parseLong(clientmessage[5]);
			messagetoclient = templateController.createTemplate(cID,name,sector,message,pID);
		}
		catch (Exception except){
			messagetoclient = null;
		}	
	}
	
	/**
	 * This method update (messagetoclient) to String array of all customersID  
	 * or to null on error 
	 */
	private void loadAllCustomers(){
		try{	
			CustomersUpdate customersupdate = new CustomersUpdate();
			messagetoclient = customersupdate.loadAllCustomers();
		}
		catch (Exception except){
			messagetoclient = null;
		}	
	}
	
	/**
	 * This method update (messagetoclient) to String array of customers details
	 * or to null on error 
	 */
	private void loadCustomer(){
		try{	
			CustomersUpdate customersupdate = new CustomersUpdate();
			messagetoclient = customersupdate.loadCustomer(Long.parseLong(clientmessage[1]));
		}
		catch (Exception except){
			messagetoclient = null;
		}	
	}
	
	/**
	 * This method update (messagetoclient) to String array of area details
	 * or to null on error 
	 */
	private void loadAllAddressZones(){
		try{	
			CustomersUpdate customersupdate = new CustomersUpdate();
			messagetoclient = customersupdate.loadAllAreas();			
		}
		catch (Exception except){
			messagetoclient = null;
		}	
	}
	
	/**
	 * This method update (messagetoclient) to "OK" if customer successfully added  
	 * to data base, or to "IlegalID" on SQL error, else to null. 
	 */
	private void addCustomer(){
		try{	
			CustomersUpdate customersupdate = new CustomersUpdate();
			long cid = Long.parseLong(clientmessage[1]);
			String name = clientmessage[2];
			int age = Integer.parseInt(clientmessage[3]);
			int area = Integer.parseInt(clientmessage[4]);
			String phone = clientmessage[5];
			if (customersupdate.addCustomer(cid, name, age, area,phone,1))
				messagetoclient = "OK";
			else
				messagetoclient = "ERROR";
		}
		catch (Exception except){
			messagetoclient = null;
		}	
	}
	
	/**
	 * This method update (messagetoclient) to "OK" if customer info successfully update  
	 * in data base, else to null. 
	 */
	private void updateCustomer(){
		try{	
			CustomersUpdate customersupdate = new CustomersUpdate();
			long cid = Long.parseLong(clientmessage[1]);
			String name = clientmessage[2];
			int age = Integer.parseInt(clientmessage[3]);
			int area = Integer.parseInt(clientmessage[4]);
			String phone = clientmessage[5];
			if (customersupdate.updateCustomer(cid, name, age, area,phone))
				messagetoclient = "OK";
			else
				messagetoclient = "ERROR";
		}
		catch (Exception except){
			messagetoclient = null;
		}	
	}
	
	/**
	 * This method update (messagetoclient) to String array of campaignID
	 * or to null on error 
	 */
	private void loadAllTemplates(){
		try{	
			TemplateController templateController = new TemplateController();
			messagetoclient = templateController.loadAllTemplates();			
		}
		catch (Exception except){
			messagetoclient = null;
		}	
	}
	
	/**
	 * This method update (messagetoclient) to String array of campaign details
	 * or to null on error 
	 */
	private void loadTemplate(){
		try{	
			TemplateController templateController = new TemplateController();
			messagetoclient = templateController.loadTemplate(Long.parseLong(clientmessage[1]));
		}
		catch (Exception except){
			messagetoclient = null;
		}	
	}
		
	/**
	 * This method update (messagetoclient) to "OK" if campaign successfully updated  
	 * in data base, or to null on SQL error. 
	 */
	private void updateTemplate(){
		try{
			TemplateController templateController = new TemplateController();
			String name, sector, message;
			long pID, cID;
			cID = Long.parseLong(clientmessage[1]);
			name = clientmessage[2];
			sector = clientmessage[3];
			message = clientmessage[4];
			pID = Long.parseLong(clientmessage[5]);
			messagetoclient = templateController.updateTemplate(cID,name,sector,message,pID);
		}
		catch (Exception except){
			messagetoclient = null;
		}	
	}
	
	/**
	 * This method update (messagetoclient) to String array of section details
	 * or to null on error 
	 */
	private void loadAllSection(){
		try{	
			CustomersUpdate customersupdate = new CustomersUpdate();
			messagetoclient = customersupdate.loadAllSection();			
		}
		catch (Exception except){
			messagetoclient = null;
		}	
	}
	
	/**
	 * This method update (messagetoclient) to String array of all sections product 
	 * details or to null on error. 
	 */
	private void loadSectionsProduct(){
		try{	
			int sID = Integer.parseInt(clientmessage[1]);
			CatalogueUpdate catalogueupdate = new CatalogueUpdate();
			messagetoclient = catalogueupdate.loadSectionsProduct(sID);	
		}
		catch (Exception except){
			messagetoclient = null;
		}	
	}
	
	/**
	 * This method update (messagetoclient) to String array of all clients 
	 * authorizations details or to null on error. 
	 */
	private void loadCustomerAuthorization(){
		try{	
			long customerID = Long.parseLong(clientmessage[1]);
			CustomersUpdate customersupdate = new CustomersUpdate();
			messagetoclient = customersupdate.loadCustomerAuthorization(customerID);	
		}
		catch (Exception except){
			messagetoclient = null;
		}	
	}
	
	/**
	 * This method update (messagetoclient) toto "OK" if the area successfully
	 *  added to data base, or to null on SQL error.  
	 */
	private void AddAddressZone(){
		int AddressZoneID = Integer.parseInt(clientmessage[1]);
		String areaName = clientmessage[2];
		CustomersUpdate customersupdate = null;
		try{	
			customersupdate = new CustomersUpdate();			
		}
		catch (Exception except){
			messagetoclient = null;
			return;
		}
		
		if (customersupdate.addArea(AddressZoneID, areaName))
			messagetoclient = "OK";
		else
			messagetoclient = "ERROR";		
	}
	
	/**
	 * This method update (messagetoclient) to "OK" if the area successfully
	 *  added to data base, or to null on SQL error.  
	 */
	private void UpdateAddressZone(){
		int areaID = Integer.parseInt(clientmessage[1]);
		String areaName = clientmessage[2];
		CustomersUpdate customersupdate = null;
		try{	
			customersupdate = new CustomersUpdate();			
		}
		catch (Exception except){
			messagetoclient = null;
			return;
		}
		
		if(customersupdate.updateArea(areaID, areaName))
			messagetoclient = "OK";
		else
			messagetoclient = "ERROR";		
	}
	
	/**
	 * This method update (messagetoclient) to array of String of all catalog sections
	 * authorizations details or to null on error. 
	 */
	private void loadAllCategorySections(){
		try{	
			CatalogueUpdate catalogueupdate = new CatalogueUpdate();
			messagetoclient = catalogueupdate.loadAllCategorySections();	
		}
		catch (Exception except){
			messagetoclient = null;
		}	
	}
	
	/**
	 * This method update (messagetoclient) to "OK" if the section successfully
	 *  added to data base, or to null on SQL error.  
	 */
	private void addCatalogueSection(){
		int sectionID=0;
		CatalogueUpdate catalogueupdate = null;
		try{	
			sectionID = Integer.parseInt(clientmessage[1]);
			catalogueupdate = new CatalogueUpdate();			
		}
		catch (Exception except){
			messagetoclient = null;
			return;
		}
		String sectionName = clientmessage[2];		
		if (catalogueupdate.addCatalogueSection(sectionID, sectionName))
			messagetoclient = "OK";
		else
			messagetoclient = "ERROR";		
	}
	
	/**
	 * This method update (messagetoclient) to "OK" if the section successfully
	 *  updated in data base, or to null on SQL error.  
	 */
	private void updateCatalogueSection(){
		int sectionID=0;
		CatalogueUpdate catalogueupdate = null;
		try{	
			sectionID = Integer.parseInt(clientmessage[1]);
			catalogueupdate = new CatalogueUpdate();			
		}
		catch (Exception except){
			messagetoclient = null;
			return;
		}
		String sectionName = clientmessage[2];		
		if (catalogueupdate.updateCatalogueSection(sectionID, sectionName))
			messagetoclient = "OK";
		else
			messagetoclient = "ERROR";		
	}
	
	/**
	 * This method update (messagetoclient) to "OK" if the section successfully
	 *  remove from data base, or to null on SQL error.  
	 */
	private void removeCatalogueSection(){
		int sectionID=0;
		CatalogueUpdate catalogueupdate = null;
		try{	
			sectionID = Integer.parseInt(clientmessage[1]);
			catalogueupdate = new CatalogueUpdate();			
		}
		catch (Exception except){
			messagetoclient = null;
			return;
		}
		if (catalogueupdate.removeCatalogueSection(sectionID))
			messagetoclient = "OK";
		else
			messagetoclient = "ERROR";		
	}
	
	/**
	 * This method update (messagetoclient) to "OK" if the type successfully
	 *  remove from data base, or to null on SQL error.  
	 */
	private void removeCatalogueType(){
		int typeID=0;
		CatalogueUpdate catalogueupdate = null;
		try{	
			typeID = Integer.parseInt(clientmessage[1]);
			catalogueupdate = new CatalogueUpdate();			
		}
		catch (Exception except){
			messagetoclient = null;
			return;
		}
		if (catalogueupdate.removeCatalogueType(typeID))
			messagetoclient = "OK";
		else
			messagetoclient = "ERROR";		
	}
	
	/**
	 * This method update (messagetoclient) to "OK" if the type successfully
	 *  updated in data base, or to null on SQL error.  
	 */
	private void updateCatalogueType(){
		int typeID=0;
		CatalogueUpdate catalogueupdate = null;
		try{	
			typeID = Integer.parseInt(clientmessage[1]);
			catalogueupdate = new CatalogueUpdate();			
		}
		catch (Exception except){
			messagetoclient = null;
			return;
		}
		String typeName = clientmessage[2];		
		if (catalogueupdate.updateCatalogueType(typeID, typeName))
			messagetoclient = "OK";
		else
			messagetoclient = "ERROR";		
	}
	
	/**
	 * This method update (messagetoclient) to "OK" if the type successfully
	 *  added to data base, or to null on SQL error.  
	 */
	private void addCataloguetype(){
		int typeID=0;
		CatalogueUpdate catalogueupdate = null;
		try{	
			typeID = Integer.parseInt(clientmessage[1]);
			catalogueupdate = new CatalogueUpdate();			
		}
		catch (Exception except){
			messagetoclient = null;
			return;
		}
		String typeName = clientmessage[2];		
		if (catalogueupdate.addCataloguetype(typeID, typeName))
			messagetoclient = "OK";
		else
			messagetoclient = "ERROR";		
	}
	
	/**
	 * This method update (messagetoclient) to array of String of all catalog types
	 * details or to null on error. 
	 */
	private void loadAllCategoryType(){
		try{	
			CatalogueUpdate catalogueupdate = new CatalogueUpdate();
			messagetoclient = catalogueupdate.loadAllCategoryType();	
		}
		catch (Exception except){
			messagetoclient = null;
		}	
	}
	
	/**
	 * This method update (messagetoclient) to String array of all type product 
	 * details or to null on error. 
	 */
	private void loadTypeProduct(){
		try{	
			int tID = Integer.parseInt(clientmessage[1]);
			CatalogueUpdate catalogueupdate = new CatalogueUpdate();
			messagetoclient = catalogueupdate.loadTypeProduct(tID);	
		}
		catch (Exception except){
			messagetoclient = null;
		}	
	}
	
	/**
	 * This method update (messagetoclient) to "OK" if the product successfully 
	 * remove from the type, or to null on SQL error.  
	 */
	private void removeProductFromType(){
		int typeID=0;
		long productID=0;
		CatalogueUpdate catalogueupdate = null;
		try{	
			typeID = Integer.parseInt(clientmessage[1]);
			productID = Long.parseLong(clientmessage[2]);
			catalogueupdate = new CatalogueUpdate();			
		}
		catch (Exception except){
			messagetoclient = null;
			return;
		}
		if (catalogueupdate.removeProductFromType(typeID, productID))
			messagetoclient = "OK";
		else
			messagetoclient = "ERROR";		
	}
	
	/**
	 * This method update (messagetoclient) to "OK" if the product successfully 
	 * added to the type, or to null on SQL error.  
	 */
	private void addProductToType()
	{
		int typeID=0;
		long productID=0;
		CatalogueUpdate catalogueupdate = null;
		try
		{	
			typeID = Integer.parseInt(clientmessage[1]);
			productID = Long.parseLong(clientmessage[2]);
			catalogueupdate = new CatalogueUpdate();			
		}catch (Exception except)
		{
			messagetoclient = null;
			return;
		}
		if (catalogueupdate.addProductToType(typeID, productID))
			messagetoclient = "OK";
		else
			messagetoclient = "ERROR";		
	}
	
	/**
	 * This method update (messagetoclient) to String array of all section types 
	 * details or to null on error. 
	 */
	private void loadAllSectionType()
	{
		try
		{	
			int cectionID = Integer.parseInt(clientmessage[1]);
			CatalogueUpdate catalogueupdate = new CatalogueUpdate();
			messagetoclient = catalogueupdate.loadAllSectionType(cectionID);	
		}catch (Exception except)
		{
			messagetoclient = null;
		}	
	}
	
	/**
	 * This method update (messagetoclient) to "OK" if the type successfully 
	 * remove from the section, or to null on SQL error.  
	 */
	private void removeTypeFromSection()
	{
		int typeID=0;
		int sectionID=0;
		CatalogueUpdate catalogueupdate = null;
		try
		{	
			sectionID = Integer.parseInt(clientmessage[1]);
			typeID = Integer.parseInt(clientmessage[2]);
			catalogueupdate = new CatalogueUpdate();			
		}catch (Exception except)
		{
			messagetoclient = null;
			return;
		}
		if (catalogueupdate.removeTypeFromSection(sectionID, typeID))
			messagetoclient = "OK";
		else
			messagetoclient = "ERROR";		
	}
	
	/**
	 * This method update (messagetoclient) to "OK" if the type successfully 
	 * added to the section, or to null on SQL error.  
	 */
	private void addTypeToSection()
	{
		int typeID=0;
		int sectionID=0;
		CatalogueUpdate catalogueupdate = null;
		try
		{	
			sectionID = Integer.parseInt(clientmessage[1]);
			typeID = Integer.parseInt(clientmessage[2]);
			catalogueupdate = new CatalogueUpdate();			
		}catch (Exception except)
		{
			messagetoclient = null;
			return;
		}
		if (catalogueupdate.addTypeToSection(sectionID, typeID))
			messagetoclient = "OK";
		else
			messagetoclient = "ERROR";		
	}
	
	/**
	 * This method update (messagetoclient) to "OK" if activeCampaign successfully updated  
	 * in data base, or to null on SQL error. 
	 */
	private void updateDealSale(){
		long activeCampaignID;
		String status, year, month, day, tmp;
		try{
			TemplateController templateUpdate = new TemplateController();
			activeCampaignID = Long.parseLong(clientmessage[1]);
			tmp = clientmessage[2];
			year = tmp.substring(0,tmp.indexOf("/"));
			tmp = tmp.substring(tmp.indexOf("/")+1, tmp.length());
			month = tmp.substring(0,tmp.indexOf("/"));
			tmp = tmp.substring(tmp.indexOf("/")+1, tmp.length());
	        day = tmp;
	        Date startdate = new Date(Integer.parseInt(year)-1900 ,Integer.parseInt(month)-1,Integer.parseInt(day));
	        tmp = clientmessage[3];
			year = tmp.substring(0,tmp.indexOf("/"));
			tmp = tmp.substring(tmp.indexOf("/")+1, tmp.length());
			month = tmp.substring(0,tmp.indexOf("/"));
			tmp = tmp.substring(tmp.indexOf("/")+1, tmp.length());
	        day = tmp;
	        Date endate = new Date(Integer.parseInt(year)-1900 ,Integer.parseInt(month)-1,Integer.parseInt(day));
			status = clientmessage[4];			
			messagetoclient = templateUpdate.updateDealSale(activeCampaignID, startdate, endate, status );
		}
		catch (Exception except){
			messagetoclient = null;
		}	
	}
	
	/**
	 * This method update (messagetoclient) to "OK" if activeCampaign successfully 
	 * inserted to data base, or to null on SQL error. 
	 */
	private void saveDealSale(){
		long activeCampaignID, campaignID;
		String status, tmp, year, month, day;
		try{
        	TemplateController templateUpdate = new TemplateController();
			activeCampaignID = Long.parseLong(clientmessage[1]);
			tmp = clientmessage[2];
			year = tmp.substring(0,tmp.indexOf("/"));
			tmp = tmp.substring(tmp.indexOf("/")+1, tmp.length());
			month = tmp.substring(0,tmp.indexOf("/"));
			tmp = tmp.substring(tmp.indexOf("/")+1, tmp.length());
	        day = tmp;
	        Date startdate = new Date(Integer.parseInt(year)-1900 ,Integer.parseInt(month)-1,Integer.parseInt(day));
	        tmp = clientmessage[3];
			year = tmp.substring(0,tmp.indexOf("/"));
			tmp = tmp.substring(tmp.indexOf("/")+1, tmp.length());
			month = tmp.substring(0,tmp.indexOf("/"));
			tmp = tmp.substring(tmp.indexOf("/")+1, tmp.length());
	        day = tmp;
	        Date endate = new Date(Integer.parseInt(year)-1900 ,Integer.parseInt(month)-1,Integer.parseInt(day));
	        status = clientmessage[4];
	        campaignID =  Long.parseLong(clientmessage[5]);
			messagetoclient = templateUpdate.saveDealSale(activeCampaignID, startdate, endate, status, campaignID);
		}
		catch (Exception except){
			messagetoclient = null;
		}	
	}
	
	/**
	 * This method update (messagetoclient) to activeCampaign details as Object[] 
	 */
	private void loadDealSale(){
		long activeCampaignID;
		try{
			TemplateController templateUpdate = new TemplateController();
			activeCampaignID = Long.parseLong(clientmessage[1]);
			messagetoclient = templateUpdate.loadActiveCampaign(activeCampaignID);
		}
		catch (Exception except){
			messagetoclient = null;
		}	
	}
	
	/**
	 * This method update (messagetoclient) to activeCampaign ID as Vector<Long> 
	 */
	private void loadAllDealSales(){
		try{
			TemplateController templateUpdate = new TemplateController();
			messagetoclient = templateUpdate.loadAllDealSales();
		}
		catch (Exception except){
			messagetoclient = null;
		}	
	}
	
	/**
	 * This function loads all customer(CustID) interests as Vector<Long> 
	 */
	private void LoadAllCustomerInterests(){
		try{
			CustomersUpdate customersUpdate = new CustomersUpdate();
			long CustID = Long.parseLong(clientmessage[1]);
			messagetoclient = customersUpdate.loadAllCustomerInterests(CustID);
		}
		catch (Exception except){
			messagetoclient = null;
		}	
	}
	
	/**
	 * This function adds a new customer interest to DB 
	 */
	private void AddCustomerInterest(){
		try{
			CustomersUpdate customersUpdate = new CustomersUpdate();
			long CustID = Long.parseLong(clientmessage[1]);
			long CatID = Long.parseLong(clientmessage[2]);
			int Rate = Integer.parseInt(clientmessage[3]);
			messagetoclient = customersUpdate.addCustomerInterest(CustID, CatID, Rate);
		}
		catch (Exception except){
			messagetoclient = null;
		}	
	}
	
	/**
	 * This method update (messagetoclient) to customers ID as Vector<Long> 
	 */
	private void loadAllclientAtSector(){
		try{
			CustomersUpdate customersUpdate = new CustomersUpdate();
			int sector = Integer.parseInt(clientmessage[1]);
			messagetoclient = customersUpdate.loadAllclientAtSector(sector);
		}
		catch (Exception except){
			messagetoclient = null;
		}	
	}
	
	/**
	 * This method update (messagetoclient) to customers ID as Vector<Long> 
	 */
	private void loadAllClientsForSalesMen(){
		try{
			SalesManController sellsMenUpdate = new SalesManController();
			messagetoclient = sellsMenUpdate.loadAllClientForSellsmen(clientmessage[1]);
		}
		catch (Exception except){
			messagetoclient = null;
		}	
	}
	
	/**
	 * This method update (messagetoclient) to sellsMen ID as Vector<Long> 
	 */
	private void loadAllSalesMen(){
		try{
			SalesManController salesmancontroller = new SalesManController();
 			messagetoclient = salesmancontroller.loadAllSalesMen();
		}
		catch (Exception except){
			messagetoclient = null;
		}	
	}
	
	/**
	 * This method update (messagetoclient) to sellsMen ID as Vector<Long> 
	 */
	private void updateSalesMenSchedule(){
		try{
			SalesManController salesmancontroller = new SalesManController();
			String schedule = clientmessage[1];
			long activeID = Long.parseLong(clientmessage[2]);
 			if (salesmancontroller.updateSalesMenSchedule(schedule, activeID))
 				messagetoclient = "OK";
 			else
 				messagetoclient = "ERROR";
		}
		catch (Exception except){
			messagetoclient = null;
		}	
	}
	
	/**
	 * This method update (messagetoclient) to sellsMen ID as Vector<Long> 
	 */
	private void loadDealSaleForClient(){
		try{
			SalesManController salesmancontroller = new SalesManController();
			long cID = Long.parseLong(clientmessage[1]);
			messagetoclient = salesmancontroller.loadDealSaleForClient(cID);
		}
		catch (Exception except){
			messagetoclient = null;
		}	
	}
	
	/**
	 * This method update (messagetoclient) to sellsMen ID as Vector<Long> 
	 */
	private void delayTemplate(){
		String tmp, year, month, day;
		try{
			SalesManController salesmancontroller = new SalesManController();
			long cID = Long.parseLong(clientmessage[1]);
			long active = Long.parseLong(clientmessage[2]);
			String message = clientmessage[3];
			tmp = clientmessage[4];
			year = tmp.substring(0,tmp.indexOf("/"));
			tmp = tmp.substring(tmp.indexOf("/")+1, tmp.length());
			month = tmp.substring(0,tmp.indexOf("/"));
			tmp = tmp.substring(tmp.indexOf("/")+1, tmp.length());
	        day = tmp;
	        Date date = new Date(Integer.parseInt(year)-1900 ,Integer.parseInt(month)-1,Integer.parseInt(day));
			if (salesmancontroller.delayCampaign(cID,active ,message, date))
				messagetoclient = "OK";
			else
				messagetoclient = "ERROR";
		}
		catch (Exception except){
			messagetoclient = null;
		}	
	}
		
	/**
	 * This method update (messagetoclient) to String on success "OK", "ERROR" on error 
	 */
	private void purchasesTemplate(){
		String tmp, year, month, day;
		try{
			SalesManController salesmancontroller = new SalesManController();
			long cID = Long.parseLong(clientmessage[1]);
			long active = Long.parseLong(clientmessage[2]);
			tmp = clientmessage[3];
			year = tmp.substring(0,tmp.indexOf("/"));
			tmp = tmp.substring(tmp.indexOf("/")+1, tmp.length());
			month = tmp.substring(0,tmp.indexOf("/"));
			tmp = tmp.substring(tmp.indexOf("/")+1, tmp.length());
	        day = tmp;
	        Date date = new Date(Integer.parseInt(year)-1900 ,Integer.parseInt(month)-1,Integer.parseInt(day));
			if (salesmancontroller.purchasesCampaign(cID,active,date))
				messagetoclient = "OK";
			else
				messagetoclient = "ERROR";
		}
		catch (Exception except){
			messagetoclient = null;
		}	
	}
	
	/**
	 * This method update (messagetoclient) to String on success "OK", "ERROR" on error 
	 */
	private void analyzesSystem(){
		try{
			CustomersUpdate customersUpdate = new CustomersUpdate();
			messagetoclient = customersUpdate.analyzesSystem();
		}
		catch (Exception except){
			messagetoclient = null;
		}	
	}
	
	/**
	 * This method update (messagetoclient) to String of response report 
	 */
	private void getResponseReport(){
		try{
			long activeID = Long.parseLong(clientmessage[1]);
			Reports reports = new Reports();
			messagetoclient = reports.getResponseReport(activeID);
		}
		catch (Exception except){
			messagetoclient = null;
		}	
	}
	
	/**
	 * This method update (messagetoclient) to String of response report 
	 */
	private void customerReport(){
		try{
			Reports reports = new Reports();
			messagetoclient = reports.customerReport();
		}
		catch (Exception except){
			messagetoclient = null;
		}	
	}
	
	/**
	 * This method update (messagetoclient) to String array of all market sectors ID  
	 * or to null on error 
	 */
	private void loadAllClientsMarket(){
		try{
			TemplateController templateController = new TemplateController();
			messagetoclient = templateController.loadAllClientsMarketSectors();
		}
		catch (Exception except){
			messagetoclient = null;
		}	
	}
	
	/**
	 * This method update (messagetoclient) to "OK" if the authorization successfully
	 *  added to data base, or to null on SQL error. 
	 */
	private void addAuthorization(){
		try{	
			int sectionID = Integer.parseInt(clientmessage[1]);
			long customerID = Long.parseLong(clientmessage[2]);
			CustomersUpdate customersupdate = new CustomersUpdate();
			if (customersupdate.addAuthorization(sectionID, customerID))
				messagetoclient = "OK";
			else
				messagetoclient = "ERROR";
		}
		catch (Exception except){
			messagetoclient = null;
		}	
	}
	
	/**
	 * This method return (messagetoclient): message that server send to client
	 */
	public synchronized Object sendMessageToClient(){
		switch(clientmessage[0]){
		case "LoadProduct":
			getProductDetails();
			return messagetoclient;
		 	
		case "AddProduct":
			insertProductDetails();
			return messagetoclient; 
			
		case "UpdateProduct":
			updateProductDetails();
			return messagetoclient;
			
		case "RemoveProduct":
			removeProduct();
			return messagetoclient;
			
		case "LoadAllProductsID":
			loadAllProductsID();
			return messagetoclient;
			
		case "LoadAllProductsNames":
			loadAllProductsNames();
			return messagetoclient;
			
		case "LoadCustomer":
			loadCustomer();
			return messagetoclient;
			
		case "LoadAllCustomers":
			loadAllCustomers();
			return messagetoclient;
			
		case "AddCustomer":
			addCustomer();
			return messagetoclient;
			
		case "UpdateCustomer":
			updateCustomer();
			return messagetoclient;
			
		case "LoadCustomerAuthorization":
			loadCustomerAuthorization();
			return messagetoclient;
			
		case "LoadTemplate":
			loadTemplate();
			return messagetoclient;
			
		case "LoadAllTemplates":
			loadAllTemplates();
			return messagetoclient;
			
		case "CreateTemplate":
			createTemplate();
			return messagetoclient;
			
		case "UpdateTemplate":
			updateTemplate();
			return messagetoclient;
			
		case "loadAllAddressZones":
			loadAllAddressZones();
			return messagetoclient;
			
		case "AddAddressZone":
			AddAddressZone();
			return messagetoclient;
			
		case "UpdateAddressZone":
			UpdateAddressZone();
			return messagetoclient;
			
		case "LoadDealSale":
			loadDealSale();
			return messagetoclient;
			
		case "LoadAllDealSales":
			loadAllDealSales();
			return messagetoclient;
			
		case "SaveDealSale":
			saveDealSale();
			return messagetoclient;
			
		case "UpdateDealSale":
			updateDealSale();
			return messagetoclient;
			
		case "LoadAllCustomerInterests":
			LoadAllCustomerInterests();
			return messagetoclient;
			
		case "AddCustomerInterest":
			AddCustomerInterest();
			return messagetoclient;
			
		case "LoadAllSection":
			loadAllSection();
			return messagetoclient;
			
		case "LoadAllCategorySections":
			loadAllCategorySections();
			return messagetoclient;
			
		case "AddCatalogueSection":
			addCatalogueSection();
			return messagetoclient;
			
		case "LoadSectionsProduct":
			loadSectionsProduct();
			return messagetoclient;
			
		case "UpdateCatalogueSection":
			updateCatalogueSection();
			return messagetoclient;
			
		case "RemoveCatalogueSection":
			removeCatalogueSection();
			return messagetoclient;
			
		case "LoadAllCategoryType":
			loadAllCategoryType();
			return messagetoclient;
			
		case "AddCataloguetype":
			addCataloguetype();
			return messagetoclient;
			
		case "UpdateCatalogueType":
			updateCatalogueType();
			return messagetoclient;
			
		case "RemoveCatalogueType":
			removeCatalogueType();
			return messagetoclient;
			
		case "LoadTypeProduct":
			loadTypeProduct();
			return messagetoclient;
			
		case "AddProductToType":
			addProductToType();
			return messagetoclient;
			
		case "RemoveProductFromType":
			removeProductFromType();
			return messagetoclient;
			
		case "LoadAllSectionType":
			loadAllSectionType();
			return messagetoclient;
			
		case "AddTypeToSection":
			addTypeToSection();
			return messagetoclient;
			
		case "RemoveTypeFromSection":
			removeTypeFromSection();
			return messagetoclient;
			
		case "LoadAllclientAtSector":
			loadAllclientAtSector();
			return messagetoclient;
			
		case "LoadAllSalesMen":
			loadAllSalesMen();
			return messagetoclient;
			
		case "loadAllClientsForSalesMen":
			loadAllClientsForSalesMen();
			return messagetoclient;
			
		case "UpdateSalesMenSchedule":
			updateSalesMenSchedule();
			return messagetoclient;
			
		case "loadDealSaleForClient":
			loadDealSaleForClient();
			return messagetoclient;
			
		case "delayTemplate":
			delayTemplate();
			return messagetoclient;
			
		case "purchasesTemplate":
			purchasesTemplate();
			return messagetoclient;
		
		case "analyzesSystem":
			analyzesSystem();
			return messagetoclient;
			
		case "customerReport":
			customerReport();
			return messagetoclient;
			
		case "getResponseReport":
			getResponseReport();
			return messagetoclient;
			
		case "LoadAllClientsMarket":
			loadAllClientsMarket();
			return messagetoclient;
			
		case "AddAuthorization":
			addAuthorization();
			return messagetoclient;
			
		default: 
			return null;
		}							
	}
}
